package com.linewow.xhyy.nihaonew.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by LXR on 2016/9/8.
 */
public class VoiceButton extends RelativeLayout {
    @Bind(R.id.view_voice_button_audio_time)
    TextView viewVoiceButtonAudioTime;
    @Bind(R.id.view_voice_button_audio_wave)
    ImageView viewVoiceButtonAudioWave;
    private LayoutInflater layoutInflater;

    private AnimationDrawable frame;
    public VoiceButton(Context context) {
        super(context);
        init(context, null);
    }

    public VoiceButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    public VoiceButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.view_widget_voice_button, this);
        ButterKnife.bind(this);
        frame=new AnimationDrawable();
        frame.addFrame(getResources().getDrawable(R.mipmap.icon_sound_wave),300);
        frame.addFrame(getResources().getDrawable(R.mipmap.icon_sound_wave1),300);
        frame.setOneShot(false);
    }

    public void setText(String time){
        viewVoiceButtonAudioTime.setText(time);
    }

    public void start(){
        viewVoiceButtonAudioWave.setBackgroundDrawable(frame);
        if(!frame.isRunning()){
            frame.start();
        }
    }
    public void stop(){
        if(frame.isRunning()){
            frame.stop();
        }
        viewVoiceButtonAudioWave.setBackgroundDrawable(getResources().getDrawable(R.mipmap.icon_sound_wave));
    }
}
